<?php
class xform{
	var $_id;

	var $fid;
	var $tpl;
	var $js;
	var $isRich = false ;
	var $T;
	var $Names;

	function __construct($id){
		$id			= (int) $id;
		$this->_id	= $id;
		C::table($this->_id);
		
		include( B_DIR . '/_res/form/' . $this->_id . '.php');
		$this->T	=& $T;
		$_T			= E::$DB->one('_table_list', 'id=' . $id );
		$_T['ShowInfo']	= @unserialize($_T['ShowInfo']);

		$this->fid	= 'F' . base_convert(rand(99,999), 16, 36);

		$this->Names	= array();
		foreach($T['field'] as $val=>&$key){
			$this->Names[$key['id']]	= $val;
		}
		unset($key);
		$this->js2	= '';
		$this->tpl = '<tbody id="' . $this->fid . '">';

		if( $_T['isGroup'] ){
			$Groups	= E::$DB->ary('_table_group', 'tid=' . $id .' ORDER BY iOrder DESC , id ASC' , 'id, name', 'id' );
			foreach($Groups as $g){
				$this->tpl	.= '<tr><td class="fileGroup" colspan="2">' . $g['name'] . '</td></tr>';
				$this->each_group( $g['id'] );
			}
		}

		$this->each_group(0);

		$this->tpl	.= '</tbody><script type="text/javascript">new XForm("' . $this->fid . '", function(){var elements = this.dom.elements; ' . $this->js . '});</script>';
		if( $this->isRich ){
			$this->tpl	= '<script type="text/javascript" src="{-$smarty.const.W_DIR-}/_admin/src/js/editor.js"></script>' .  $this->tpl;
		}
		file_put_contents( B_DIR . '/_res/form/' . $this->_id . '.tpl' , $this->tpl);
	}

	function each_group( $_gid ){
		foreach($this->T['field'] as $fid=>&$field){
			if( $_gid != $field['gid'] ){
				continue;
			}
			$field['need'] = @unserialize($field['need']);
			$attrs	= '';
			if(is_array( $field['need'] )){
				$attrs	.= ' style="display:none;"';
				$js 	= array();
				foreach($field['need'] as $k=>$v){
					$js[]	= $this->Names[$k] . ':[' . join(',', $v) . ']';
				}
				$js = join(',', $js);
				$attrs	.=	' depends="' . $js . '"';
			}
			
			if( $field['elementType'] == 'xhr' ){
				$attrs	.= ' depField="' . $this->Names[ $field['DepField'] ] . '"';
			}

			
			$field['note']	= $field['note'] ? '<a class="note">' . $field['note'] . '</a>' :"";
			if( $field['elementType'] == 'select' || $field['elementType'] == 'select' ){
				$field['size']	= $field['size'] ? ' style="width:' . ($field['size']  + 5) . 'ex;"' : '';
			}else{
				$field['size']	= $field['size'] ? ' size="' . $field['size'] . '" maxlength="' . $field['size'] . '"' :'';
			}

			call_user_func( array($this, 'build_'.$field['elementType']), $field, $attrs);
			$this->tpl	.= "\n";
		}
	}

	function build_checkbox(&$field ,&$attrs){

		$this->tpl .= '<tr' . $attrs . ' class="checkbox" val="{-foreach from=$L.' . $field['fieldKey'] . ' item=a-}{-$a-},{-/foreach-}"><td class="fieldName">' . $field['fieldName'] . '</td><td class="fieldValue">';
		foreach($field['child'] as $val=>&$var){
			$this->tpl .= '<label for="' . $this->fid . '_' . $field['id'] . '_' . $val . '"><input type="checkbox" id="' . $this->fid . '_' . $field['id'] . '_' . $val . '" name="' . $field['fieldKey'] . '[]" value="' . $val . '"/>' . $var . '</label>';
		}
		unset($var);
		$this->tpl  .= '</td></tr>';
		if( empty($field['alert']) ){
			$field['alert']	= '至少要选择一个' . $field['fieldName'] . '选项!';
		}
		if ( $field['isForce'] ){
			$this->addCheckedValidate($field);
		}

	}

	function build_file(&$field ,&$attrs){

		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td class="fieldValue"><input type="file" name="' . $field['fieldKey'] . '"' . $size . ' /> ' . $field['note'] . ' {-if $L.' . $field['fieldKey'] . '-}<a target="_blank" href="{-$L.' . $field['fieldKey'] . '-}">察看</a> <a class="note">再次上传覆盖旧的</a>{-/if-}</td></tr>';

		$xxx	= $field['regs'] ? ' !e.value.match(/' . $field['regs'] . '/i)':' e.value =="" ';

		if( $field['isForce'] || $field['regs'] ){
			$this->js .= '
			try{
				var e = elements["' . $field['fieldKey'] . '"];
				if( ' . $xxx . ' ){
					alert("' . $field['fieldName'] . ' 验证提示:\n  ' . $field['alert'] . '");
					e.select(); e.focus(); return true;
				}
			}catch(err){ return true;}';
		}
	}

	function build_password(&$field ,&$attrs){
		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td><input type="password" name="' . $field['fieldKey'] . '" value="{-$L.' . $field['fieldKey'] . '-}"' . $field['size'] . ' /> ' . $field['note'] . '</td></tr>';
	}

	function build_radio(&$field ,&$attrs){
		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td>';
		foreach($field['child'] as $val=>&$var){
			$this->tpl .= '<label for="' . $this->fid . '_' . $field['id'] . '_' . $val . '"><input type="radio"   id="' . $this->fid . '_' . $field['id'] . '_' . $val . '" name="' . $field['fieldKey'] . '" value="' . $val . '"{-if "' . $val . '"==$L.' . $field['fieldKey'] . '-} checked="checked"{-/if-} />' . $var . '</label>';
		}
		unset($var);
		$this->tpl .= '</td></tr>';
		if( empty($field['alert']) ){
			$field['alert']	= '您必须择' . $field['fieldName'] . '选项';
		}
		if ( $field['isForce'] ){
			$this->addCheckedValidate($field);
		}
	}


	function build_richtext(&$field ,&$attrs){
		$id	= $this->fid . 'r' . $field['id'];
		$this->tpl	.= '<tr'.$attrs.'><td class="fieldName">'.$field['fieldName'].'</td><td class="oEditor"><textarea  name="'.$field['fieldKey'].'" id="r'.$id.'" >{-$L.'.$field['fieldKey'].'|escape-}</textarea>';
		$this->tpl	.= '<script type="text/javascript">AddEditor("oEdit' . $id . '", "r' . $id . '", '. $this->_id .',{-$L.id|default:0-},{-php-}$t = time(); echo $t . ",\'" . md5( \''. $this->_id.'\'.( (int) $this->_tpl_vars[\'L\'][\'id\']) .$this->_tpl_vars[\'html\'][\'whisper\'] .$t) {-/php-}\');</script></td></tr>';
		$this->isRich	= true;
	}

	function build_select(&$field ,&$attrs){
		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td><select name="' . $field['fieldKey'] . '"' . $field['size'] . '>';
		
		foreach($field['child'] as $val => &$var ){
			$this->tpl .= '<option value="' . $val . '"{-if "' . $val . '"==$L.' . $field['fieldKey'] . '-} selected="selected" style="color:red;"{-/if-}>' . $var . '</option>';
		}
		unset($var);
		$this->tpl .= '</select></td></tr>';
	}

	function build_text(&$field ,&$attrs){
		
		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td><input type="text" name="' . $field['fieldKey'] . '" value="{-$L.' . $field['fieldKey'] . '|escape-}"' . $field['size'] . '/> ' . $field['note'] . '</td></tr>';

		$xxx	= $field['regs'] ? ' !e.value.match(/' . $field['regs'] . '/)':' e.value =="" ';
		$pat = array();
		if( preg_match('/mail/i', $field['fieldKey']) ){
			$pat[]	= '!e.value.match(/^[\w\_\.\-]{2,22}\@([\w\-]{2,30}\.){1,4}[a-z]{2,5}$|^$/i)';
			if( empty($field['alert']) ){
				$field['alert'] = '请输入正确的Emal地址!';
			}
		}else{
			if($field['regs']){
				$pat[]	= ' !e.value.match(/' . $field['regs'] . '/i)';
			}
		}
		if( $field['isForce'] ){
			$pat[]	= 'e.value.match(/^\s*$/)';
		}
		if( count($pat) ){
			$pat	= join(' || ', $pat);
			$msg	= $field['alert'] ? $field['alert'] : '请输入' . $field['fieldName'] . '!';
			$this->js  .= '
			try{
				var e = elements["' . $field['fieldKey'] . '"];
				if( '.$pat.' ){
					alert("' . $msg . '"); e.select();e.focus(); return true;
				}
			}catch(e){ return true;}';
		}
	}

	function build_textarea(&$field ,&$attrs){
		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td><textarea name="' . $field['fieldKey'] . '" style="width:99%;height:180px;">{-$L.' . $field['fieldKey'] . '|escape-}</textarea></td></tr>';


		$xxx	= $field['regs'] ? ' !e.value.match(/' . $field['regs'] . '/)' : ' e.value =="" ';

		if( $field['isForce'] || $field['regs'] ){
			$this->js  .= '
			try{
				var e = elements["'.$field['fieldKey'].'"];
				if( ' . $xxx . ' ){
					alert("' . $field['fieldName'] . ' 验证提示:\n  ' . $field['alert'] . '"); e.select(); e.focus();return true;
				}
			}catch(err){return true;}';
		}
	}


	function build_xhr(&$field ,&$attrs){
		$this->tpl .= '<tr' . $attrs . '><td class="fieldName">' . $field['fieldName'] . '</td><td class="fieldValue"><select name="' . $field['fieldKey'] . '" val="{-$L.' . $field['fieldKey'] . '-}"' . $field['size'] . '></select></td></tr>';
	}

	function addCheckedValidate(&$field){
		$name		=  ( $field['elementType'] == 'checkbox' ) ? $field['fieldKey'].'[]' : $field['fieldKey'];
		$this->js	.= '
		try{
			var o = elements["' . $name . '"];
			if(!Array.some(o, function(o){
				return o.checked;
			})){
				alert("' . $field['fieldName'] . ' 验证提示:\n  ' . $field['alert'] . '");
				o.item(0).focus(); return true;
			}
		}catch(e){ return true;}';
	}
}
?>